﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SharedGenomics.Core
{
    public class PropertyValidationErrorCollection
    {
        private readonly IDictionary<string, IList<PropertyValidationError>> _propertyErrors;

        public PropertyValidationErrorCollection()
        {
            this._propertyErrors = new Dictionary<string, IList<PropertyValidationError>>();
        }

        public void AddError(string propertyName, string errorMessage)
        {
            propertyName.ThrowIfNull("propertyName");
            this.AddError(new PropertyValidationError(propertyName, errorMessage));
        }

        public void AddError(PropertyValidationError validationError)
        {
            validationError.ThrowIfNull("validationError");

            var errorList = this._propertyErrors.GetValueForSafe(validationError.PropertyName, () => new List<PropertyValidationError>());
            errorList.Add(validationError);
        }

        public bool ContainsErrorFor(string propertyName)
        {
            return propertyName != null && this._propertyErrors.ContainsKey(propertyName);
        }

        public IEnumerable<PropertyValidationError> GetErrorsFor(string propertyName)
        {
            return this.ContainsErrorFor(propertyName)
                ? this._propertyErrors[propertyName].AsEnumerable()
                : Enumerable.Empty<PropertyValidationError>();
        }

        public bool IsValid
        {
            get { return this._propertyErrors.Count == 0; }
        }
    }
}
